#include <sys/types.h>
#include<sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
    pid_t pid = fork();
    if (pid == 0) // fork函数返回值：子进程返回0
    {
        // Child
        printf("Child: pid is %d, parent: pid is %d\n", getpid(), getppid());
        return 0;
    }
    else if (pid == -1)
    {
        // error
        perror("fork()");
        exit(1);
    }
    //wait
    int status = 0;
    //waitpid(-1,&status,0); 和 wait(&status) 一样
    pid_t cpid = waitpid(pid,&status,0);//pid 是子进程id,
    //cpid是正常返回的时候waitpid返回收集到的子进程的进程ID

    // parent
    // fork函数返回值:父进程返回的是子进程的pid
    printf("parent :pid is %d, fork return %d\n", getpid(), pid);
    printf("child :pid is %d, exit code:%d,sign code:%d\n", cpid,(status>>8)&0xff,(status)&0x7f);
    return 0;
}